<?
if ($cl_count != 0) {
	echo "<script language='JavaScript'>
<!--
var myWin = '';


function openWin(url, name, properties) {
    if (myWin && myWin.open && !myWin.closed) {
	myWin.close();
    }
    myWin = window.open(url, name, properties);
    return false;
}
//-->
</script>";
    //echo "klienti ne mozhe da se editvat!";
    $params[sid] = $sid;
	$params[action] = "showPlanClientsDetails";
	$params[rid] = $id;
	$params[pid] = $prod_id;
	$detailsurl2 = "$baseURL/$webDIR/?".encode_params($params);

	$params[sid] = $sid;
	$params[action] = "createPlan";
	$createurl = "$baseURL/$webDIR/?".encode_params($params);

	$params[sid] = $sid;
	$params[action] = "moveClient2Plan";
	$moveurl = "$baseURL/$webDIR/?".encode_params($params);
    $kkk = $cl_count;
    $errorMsg = translate('delete_plan_impossible' , array('numberofclients' => $kkk,
    														'planname' => $plname,
    														'details' => $detailsurl2,
    														'create' => $createurl,
    														'move' => $moveurl));
    include_once('errorMsg.html');
	$hide_rest = 'yes';
} else {
	//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='plan_id';
	$sql['table']='products';
	$sql['where']="product_id='$prod_id'";
	$sql['options']='';
	$sql['return']='struct';
	//------------------------------------------------------
	if (!$pres=sql_exec($sql,$dbh81,'assoc')) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
	}
	$plan_id = $pres[0]['plan_id'];
	if (in_array($plan_id,array('b','c','p'))) {
		$errorMsg = translate('delete_impossible');
		include_once('errorMsg.html');
		echo "<br>";
	} else {
		$sql = "SELECT store_id FROM products WHERE product_id = '$prod_id' AND reseller_id = '$id'";
		$res = pg_query($dbh81,$sql);
		if (!$res) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}
		$store_id = pg_fetch_result($res,0,'store_id');
		unset($sql,$res);
		$sql = "SELECT products FROM stores WHERE store_id = '$store_id'";
		$res = pg_query($dbh81,$sql);
		if (!$res) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}
		$prods = unserialize(base64_decode(pg_fetch_result($res,0,'products')));
		$domain_free = array_keys($prods['domains_free']);
		if (in_array($prod_id,$domain_free)) {
			foreach ($domain_free as $key => $value) {
				if ($value != $prod_id) {
					$tmp_arr[$key] = $value;
				}
			}
			unset($key,$value);
			foreach ($tmp_arr as $value) {
				$tmp_arr2[$value][12] = 'on';
			}
			unset($key,$value);
			foreach ($prods as $key => $value) {
				if ($key == 'domains_free') {
					$value = $tmp_arr2;
				}
				$new_prods[$key] = $value;
			}
		} else {
			$new_prods = $prods;
		}
		unset($key,$value,$tmp_arr);
		$plans_offered = array_keys($new_prods['plans_offering']);
		if (in_array($prod_id,$plans_offered)) {
			foreach ($plans_offered as $key => $value) {
				if ($value != $prod_id) {
					$tmp_arr[$value] = 'on';
				}
			}
			unset($key,$value);
			foreach ($new_prods as $key => $value) {
				if ($key == 'plans_offering') {
					$value = $tmp_arr;
				}
				$new_prods[$key] = $value;
			}
		}
		unset($key,$value,$tmp_arr);
		$plans_on_index = array_keys($new_prods['plans_on_index']);
		if (in_array($prod_id,$plans_on_index)) {
			foreach ($plans_on_index as $key => $value) {
				if ($value != $prod_id) {
					$tmp_arr[$value] = 'on';
				}
			}
			unset($key,$value);
			foreach ($new_prods as $key => $value) {
				if ($key == 'plans_on_index') {
					$value = $tmp_arr;
				}
				$new_prods[$key] = $value;
			}
		}
		unset($key,$value,$tmp_arr);
		$periods = $new_prods['periods'];
		foreach ($periods as $key => $value) {
			if ($key != $prod_id) {
				$tmp_arr[$key] = $value;
			}
		}
		$new_prods['periods'] = $tmp_arr;
		$new_prods = base64_encode(serialize($new_prods));
		pg_query($dbh81,'BEGIN');
		$sql = "UPDATE stores SET products = '$new_prods' WHERE store_id = '$store_id'";
		$res = pg_query($dbh81,$sql);
		if (!$res) {
			pg_query($dbh81,'ROLLBACK');
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}
		unset($sql,$res);
		$sql="DELETE FROM products WHERE product_id='$prod_id' AND reseller_id='$id'";
		$res=pg_query($dbh81,$sql);
		if (!$res) {
			pg_query($dbh81,'ROLLBACK');
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}

		pg_query($dbh81,'COMMIT');
		$pname=$plan_name;
		$okMsg = translate('successfully_deleted_plan' , array('plana' => $pname));
		include_once('okMsg.html');
	}
}
?>
